package copyengine.utils.tick.node
{
	import copyengine.resource.lazyload.ICELazyLoadContainer;
	import copyengine.utils.debug.CELog;

	import flash.display.DisplayObjectContainer;

	public class AnimationTickObjectNodeBasic extends TickObjectNode
	{
		//结束后直接返回 不做任何操作
		public static const END_ACTION_DO_NOTHING:int = 0;
		//结束后删除Target
		public static const END_ACTION_REMOVE:int = 1;
		//结束后隐藏Target
		public static const END_ACTION_INVISIBLE:int = 2
		//结束后Stop住Target
		public static const END_ACTION_STOP:int = 3;

		protected var target:DisplayObjectContainer;
		protected var endAction:int;

		private var mcTotalFrame:int;
		//当前Tick到的帧数
		private var currentPlayedFrame:int;
		//一共需要Tick的帧数
		protected var totalPlayFrame:int = 1;
		//当前是否可以开始Tick(如果是Lazyload需要等待load完成)
		private var isCanStartTick:Boolean;

		private var allMiddleTickCallBackDataVector:Vector.<AnimationTickDataWarp>;

		public function AnimationTickObjectNodeBasic(_target:DisplayObjectContainer, _callBackFunction:Function, _repeatTime:int, _endAction:int, _mcTotalFrame:int)
		{
			target = _target;
			endAction = _endAction;
			mcTotalFrame = _mcTotalFrame;
			super(_callBackFunction, 1, _repeatTime);
			start();
		}

		//============================================//
		//==Initialize&Dispose  Function
		//============================================//
		override final public function destory():void
		{
			doDispose();
			super.destory();
		}

		//============================================//
		//==Public Interface  Function
		//============================================//
		public final function addMiddleTickCallBack(_f:Function, _callBackFrame:int = int.MIN_VALUE, _callBackPercent:int = int.MIN_VALUE):void
		{
			if (allMiddleTickCallBackDataVector == null)
			{
				allMiddleTickCallBackDataVector = new Vector.<AnimationTickDataWarp>();
			}
			var dataWarp:AnimationTickDataWarp = new AnimationTickDataWarp();
			dataWarp.callBackFrame = _callBackFrame;
			dataWarp.callBackPercent = _callBackPercent;
			dataWarp.callBackFunction = _f;
			//在AddMiddleCallBack时候可能已经初始化好TotalFrame,此时则设置该值
			//如果没有初始化好,在后面也会调用initializeTickData重新初始化
			dataWarp.totalFrame = totalPlayFrame;
			allMiddleTickCallBackDataVector.push(dataWarp);
		}

		//============================================//
		//==OverrideAble  Function
		//============================================//
		protected function getTargetTotalFrame():int
		{
			CELog.err("Child Class Should Override this function ")
			return 1;
		}

		protected function doTickTarget(_currentPlayFrame:int):Boolean
		{
			return false;
		}

		protected function doStartTick():void
		{
		}

		protected function doDispose():void
		{
		}

		//============================================//
		//==Private  Function
		//============================================//

		private function start():void
		{
			if (target is ICELazyLoadContainer)
			{
				(target as ICELazyLoadContainer).addLoadComplateCallBackFunction(onLazyLoadFileLoadComplate);
			}
			else
			{
				isCanStartTick = true;
				initializeTickData();
				doStartTick();
			}
		}

		private function onLazyLoadFileLoadComplate():void
		{
			isCanStartTick = true;
			initializeTickData();
			doStartTick();
		}

		private function initializeTickData():void
		{
			currentPlayedFrame = 1;
			totalPlayFrame = mcTotalFrame < 0 ? getTargetTotalFrame() : mcTotalFrame;
			if (allMiddleTickCallBackDataVector != null)
			{
				for each (var dataWarp:AnimationTickDataWarp in allMiddleTickCallBackDataVector)
				{
					dataWarp.totalFrame = totalPlayFrame;
				}
			}
		}

		override final protected function tickLogic(_elapseTime:int):void
		{
			if (isCanStartTick)
			{
				//在父类中增加当前Tick的帧数
				//且尝试回调MiddleCallBack
				currentPlayedFrame++;
				tickMiddleCallBackItem(currentPlayedFrame);
				if (doTickTarget(currentPlayedFrame))
				{
					//Supre的Tick一旦调用则当前Animation会结束
					super.tickLogic(_elapseTime);
					//当回调super.tick函数则认为Animation已经Tick结束，再
					//父类的TickLoic函数执行完毕后(@see super.tick)会根据是否完成更改
					//remainRepeatTime，所以如果此时remainRepeatTime=1则回调super后当前结束
					//但是如果>1则当前还会继续repeat
					if (remainRepeatTime > 1)
					{
						initializeTickData();
						doStartTick();
					}
				}
			}
		}

		private function tickMiddleCallBackItem(_currentTickCount:int):void
		{
			if (allMiddleTickCallBackDataVector != null)
			{
				for (var j:int = allMiddleTickCallBackDataVector.length - 1; j >= 0; j--)
				{
					if (allMiddleTickCallBackDataVector[j].tick(_currentTickCount))
					{
						allMiddleTickCallBackDataVector.splice(j, 1);
					}
				}
			}
		}


	}
}
